using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using ICSharpCode.TextEditor.Document;

namespace DukeyCssFetch
{
	// Token: 0x0200000D RID: 13
	public class TextEditorSearcher : IDisposable
	{
		// Token: 0x06000095 RID: 149 RVA: 0x0000AA8C File Offset: 0x00008C8C
		public TextEditorSearcher()
		{
			
		}

		// Token: 0x06000088 RID: 136 RVA: 0x0000A768 File Offset: 0x00008968
		public void ClearScanRegion()
		{
			if (this.textMarker_0 != null)
			{
				this.idocument_0.MarkerStrategy.RemoveMarker(this.textMarker_0);
				this.textMarker_0 = null;
			}
		}

		// Token: 0x06000089 RID: 137 RVA: 0x0000A790 File Offset: 0x00008990
		public void Dispose()
		{
			this.ClearScanRegion();
			GC.SuppressFinalize(this);
		}

		// Token: 0x0600008A RID: 138 RVA: 0x0000A7A0 File Offset: 0x000089A0
		~TextEditorSearcher()
		{
			this.Dispose();
		}

		// Token: 0x0600008F RID: 143 RVA: 0x0000A820 File Offset: 0x00008A20
		public TextRange FindNext(int beginAtOffset, bool searchBackward, out bool loopedAround)
		{
			loopedAround = false;
			int beginOffset = this.BeginOffset;
			int endOffset = this.EndOffset;
			int num = Globals.InRange(beginAtOffset, beginOffset, endOffset);
			this.BdlStElWyT = (this.MatchCase ? this.string_0 : this.string_0.ToUpperInvariant());
			TextRange textRange;
			if (searchBackward)
			{
				textRange = this.method_0(beginOffset, num, true);
				if (textRange == null)
				{
					loopedAround = true;
					textRange = this.method_0(num, endOffset, true);
				}
			}
			else
			{
				textRange = this.method_0(num, endOffset, false);
				if (textRange == null)
				{
					loopedAround = true;
					textRange = this.method_0(beginOffset, num, false);
				}
			}
			return textRange;
		}

		// Token: 0x06000090 RID: 144 RVA: 0x0000A8A4 File Offset: 0x00008AA4
		private TextRange method_0(int int_0, int int_1, bool bool_0)
		{
			int_1 -= this.string_0.Length;
			TextEditorSearcher.Func<char, char, bool> func;
			if (this.MatchCase)
			{
				if (TextEditorSearcher.func_0 == null)
				{
					TextEditorSearcher.func_0 = new TextEditorSearcher.Func<char, char, bool>(TextEditorSearcher.smethod_0);
				}
				func = TextEditorSearcher.func_0;
			}
			else
			{
				if (TextEditorSearcher.func_1 == null)
				{
					TextEditorSearcher.func_1 = new TextEditorSearcher.Func<char, char, bool>(TextEditorSearcher.smethod_1);
				}
				func = TextEditorSearcher.func_1;
			}
			TextEditorSearcher.Func<int, bool> func2;
			if (this.MatchWholeWordOnly)
			{
				func2 = new TextEditorSearcher.Func<int, bool>(this.method_1);
			}
			else
			{
				func2 = new TextEditorSearcher.Func<int, bool>(this.method_4);
			}
			char arg = this.BdlStElWyT[0];
			if (bool_0)
			{
				for (int i = int_1; i >= int_0; i--)
				{
					if (func(arg, this.idocument_0.GetCharAt(i)) && func2(i))
					{
						return new TextRange(this.idocument_0, i, this.string_0.Length);
					}
				}
			}
			else
			{
				for (int j = int_0; j <= int_1; j++)
				{
					if (func(arg, this.idocument_0.GetCharAt(j)) && func2(j))
					{
						return new TextRange(this.idocument_0, j, this.string_0.Length);
					}
				}
			}
			return null;
		}

		// Token: 0x06000091 RID: 145 RVA: 0x0000A9C8 File Offset: 0x00008BC8
		private bool method_1(int int_0)
		{
			return this.method_2(int_0) && this.method_2(int_0 + this.string_0.Length) && this.method_4(int_0);
		}

		// Token: 0x06000092 RID: 146 RVA: 0x0000A9F4 File Offset: 0x00008BF4
		private bool method_2(int int_0)
		{
			return int_0 <= 0 || int_0 >= this.idocument_0.TextLength || !this.method_3(int_0 - 1) || !this.method_3(int_0);
		}

		// Token: 0x06000093 RID: 147 RVA: 0x0000AA20 File Offset: 0x00008C20
		private bool method_3(int int_0)
		{
			char charAt = this.idocument_0.GetCharAt(int_0);
			return char.IsLetterOrDigit(charAt) || charAt == '_';
		}

		// Token: 0x06000094 RID: 148 RVA: 0x0000AA4C File Offset: 0x00008C4C
		private bool method_4(int int_0)
		{
			string text = this.idocument_0.GetText(int_0, this.string_0.Length);
			if (!this.MatchCase)
			{
				text = text.ToUpperInvariant();
			}
			return text == this.BdlStElWyT;
		}

		// Token: 0x06000085 RID: 133 RVA: 0x0000A6DC File Offset: 0x000088DC
		public void SetScanRegion(ISelection sel)
		{
			this.SetScanRegion(sel.Offset, sel.Length);
		}

		// Token: 0x06000086 RID: 134 RVA: 0x0000A6F0 File Offset: 0x000088F0
		public void SetScanRegion(int offset, int length)
		{
			Color backgroundColor = this.idocument_0.HighlightingStrategy.GetColorFor("Default").BackgroundColor;
			this.textMarker_0 = new TextMarker(offset, length, TextMarkerType.SolidBlock, Globals.HalfMix(backgroundColor, Color.FromArgb(160, 160, 160)));
			this.idocument_0.MarkerStrategy.AddMarker(this.textMarker_0);
		}

		// Token: 0x06000096 RID: 150 RVA: 0x0000AA9C File Offset: 0x00008C9C
		[CompilerGenerated]
		private static bool smethod_0(char char_0, char char_1)
		{
			return char_0 == char_1;
		}

		// Token: 0x06000097 RID: 151 RVA: 0x0000AAA4 File Offset: 0x00008CA4
		[CompilerGenerated]
		private static bool smethod_1(char char_0, char char_1)
		{
			return char_0 == char.ToUpperInvariant(char_1);
		}

		// Token: 0x17000006 RID: 6
		public int BeginOffset
		{
			// Token: 0x0600008B RID: 139 RVA: 0x0000A7D0 File Offset: 0x000089D0
			get
			{
				if (this.textMarker_0 != null)
				{
					return this.textMarker_0.Offset;
				}
				return 0;
			}
		}

		// Token: 0x17000004 RID: 4
		public IDocument Document
		{
			// Token: 0x06000083 RID: 131 RVA: 0x0000A6BC File Offset: 0x000088BC
			get
			{
				return this.idocument_0;
			}
			// Token: 0x06000084 RID: 132 RVA: 0x0000A6C4 File Offset: 0x000088C4
			set
			{
				if (this.idocument_0 != value)
				{
					this.ClearScanRegion();
					this.idocument_0 = value;
				}
			}
		}

		// Token: 0x17000007 RID: 7
		public int EndOffset
		{
			// Token: 0x0600008C RID: 140 RVA: 0x0000A7E8 File Offset: 0x000089E8
			get
			{
				if (this.textMarker_0 != null)
				{
					return this.textMarker_0.EndOffset;
				}
				return this.idocument_0.TextLength;
			}
		}

		// Token: 0x17000005 RID: 5
		public bool HasScanRegion
		{
			// Token: 0x06000087 RID: 135 RVA: 0x0000A758 File Offset: 0x00008958
			get
			{
				return this.textMarker_0 != null;
			}
		}

		// Token: 0x17000008 RID: 8
		public string LookFor
		{
			// Token: 0x0600008D RID: 141 RVA: 0x0000A80C File Offset: 0x00008A0C
			get
			{
				return this.string_0;
			}
			// Token: 0x0600008E RID: 142 RVA: 0x0000A814 File Offset: 0x00008A14
			set
			{
				this.string_0 = value;
			}
		}

		// Token: 0x040000D4 RID: 212
		private string BdlStElWyT;

		// Token: 0x040000D5 RID: 213
		[CompilerGenerated]
		private static TextEditorSearcher.Func<char, char, bool> func_0;

		// Token: 0x040000D6 RID: 214
		[CompilerGenerated]
		private static TextEditorSearcher.Func<char, char, bool> func_1;

		// Token: 0x040000CF RID: 207
		private IDocument idocument_0;

		// Token: 0x040000D1 RID: 209
		public bool MatchCase;

		// Token: 0x040000D2 RID: 210
		public bool MatchWholeWordOnly;

		// Token: 0x040000D3 RID: 211
		private string string_0;

		// Token: 0x040000D0 RID: 208
		private TextMarker textMarker_0;

		// Token: 0x0200000F RID: 15
		// Token: 0x0600009D RID: 157
		public delegate TResult Func<T, TResult>(T arg);

		// Token: 0x0200000E RID: 14
		// Token: 0x06000099 RID: 153
		public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
	}
}
